﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BUS;
using CrystalDecisions.Enterprise;

public partial class User_XuatHang : System.Web.UI.Page
{
    DataTable dtTenSP;
    DataTable dtKH;
    String makh;
    PhieuXuatHang ph = new PhieuXuatHang();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");

        if (!IsPostBack)
        {
            cmbTenSP.DataSource = dtTenSP = XuatHangBUS.LayDanhSachTenSP();
            cmbTenSP.DataValueField = "TenSP";
            cmbTenSP.DataBind();
            bindgrid();
            if (float.Parse(lbTongTien.Text) == 0)
                btnXuatHang.Visible = false;
        }
    }
    protected void bindgrid()
    {
        float TongTien = 0;
        ListSP_Xuat listsp;
        listsp = (ListSP_Xuat)Session["XuatHang"];
        if (listsp == null)
            listsp = new ListSP_Xuat();
        gvSanPham.DataSource = listsp.sp;
        gvSanPham.DataBind();
        for (int i = 0; i < listsp.sp.Count; i++)
        {
            TongTien = TongTien + listsp.sp[i].thanhtien;
        }
        if (TongTien > 0)
        {
            btnXuatHang.Visible = true;
        }
        lbTongTien.Text = TongTien.ToString();
    }
    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    protected void btnThemSP_Click(object sender, EventArgs e)
    {
        float sl = float.Parse(txtSoLuong.Text);
        String masp = XuatHangBUS.LayMaSanPham(cmbTenSP.Text);
        float slcon = XuatHangBUS.SoLuongSPCon(masp);
        if (sl > slcon)
        {
            Response.Write("<script language='javascript'>alert(\"Trong kho còn " + slcon + " sản phẩm. Thiếu hàng trong kho\");</script>");
        }
        else
        {
            ListSP_Xuat listsp;
            listsp = (ListSP_Xuat)Session["XuatHang"];
            if (listsp == null)
                listsp = new ListSP_Xuat();

            SanPham_Xuat sp = new SanPham_Xuat();
            sp.masp = masp;
            sp.tensp = cmbTenSP.Text;
            sp.dongia = float.Parse(txtDonGia.Text);
            sp.soluong = sl;
            sp.thanhtien = float.Parse(txtDonGia.Text) * float.Parse(txtSoLuong.Text);
            listsp.ThemSP(sp);
            Session["XuatHang"] = listsp;
            bindgrid();
            gotoAnchorScript("form");
        }
    }
    protected void txtNhapMaKH_Click(object sender, EventArgs e)
    {
        makh = txtMaKH.Text;
        dtKH = XuatHangBUS.LayThongTinKhachHang(makh);
        if (dtKH.Rows.Count > 0)
        {
            txtTenKH.Text = dtKH.Rows[0]["TenKH"].ToString();
            txtDiaChiKH.Text = dtKH.Rows[0]["DiaChi"].ToString();
            txtSdtKH.Text = dtKH.Rows[0]["SoDienThoai"].ToString();

            Image1.ImageUrl = "~/Images/Ok-icon.png";
            Label1.Text = "Mã số khách hàng hợp lệ";
            Label1.ForeColor = System.Drawing.Color.Blue;
            Label1.Visible = true;
            Image1.Visible = true;
        }
        else
        {
            txtMaKH.Text = "";
            Image1.ImageUrl = "~/Images/Symbols-Error-icon.png";
            Label1.Text = "Mã số khách hàng không hợp hợp lệ";
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Visible = true;
            Image1.Visible = true;
        }
    }

    protected void gvSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Int32 idx = Int32.Parse(e.CommandArgument.ToString());
        GridViewRow row = gvSanPham.Rows[idx];
        String masp = row.Cells[0].Text;
        float soluong = float.Parse(((TextBox)row.Cells[2].FindControl("gv_soluong")).Text);
        float dongia = float.Parse(((TextBox)row.Cells[3].FindControl("gv_dongia")).Text);
        if (e.CommandName.Equals("CapNhat"))
        {
            float slcon = XuatHangBUS.SoLuongSPCon(masp);
            if (soluong > slcon)
            {
                Response.Write("<script language='javascript'>alert(\"Trong kho còn " + slcon + " sản phẩm. Thiếu hàng trong kho\");</script>");
                ((TextBox)row.Cells[2].FindControl("gv_soluong")).Text = (float.Parse(row.Cells[4].Text) / dongia).ToString();
            }
            else
            {
                ListSP_Xuat listsp;
                listsp = (ListSP_Xuat)Session["XuatHang"];
                listsp.CapNhatSP(masp, soluong, dongia);
                Session["XuatHang"] = listsp;
                bindgrid();
            }
        }
        gotoAnchorScript("list");
    }
    protected void btnXuatHang_Click(object sender, EventArgs e)
    {
        ph.tenkh = txtTenKH.Text.Trim();
        ph.diachi = txtDiaChiKH.Text.Trim();
        ph.sodienthoai = txtSdtKH.Text.Trim();

        int giamgia = XuatHangBUS.LayGiamGia(ph.tenkh,ph.diachi);
        ph.giamgia = float.Parse(txtGiamGia.Text.Trim()) + ((float.Parse(lbTongTien.Text)) * giamgia / 100);

        ph.tennv = NhanVienBUS.LayTenNhanVien((String)Session["UserName"]);
        ph.ngayxuat =  DateTime.Now;
        ph.tongtien = float.Parse(lbTongTien.Text);
        ListSP_Xuat list = (ListSP_Xuat)Session["XuatHang"];

        XuatHangBUS.XuatHang(ph, list);
        Session["MaHD"] = XuatHangBUS.LayMaPhieuXuat();
        Response.Redirect("InHoaDon.aspx"); 
    }
    protected void gvSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String masp = gvSanPham.Rows[e.RowIndex].Cells[0].Text;
        ListSP_Xuat listsp;
        listsp = (ListSP_Xuat)Session["XuatHang"];
        listsp.XoaSP(masp);
        Session["XuatHang"] = listsp;
        bindgrid();
        gotoAnchorScript("list");
    }
}
